package com.hdjk.view.callback;

import com.hdjk.view.entity.BankEntity;
import com.lst.projectlib.entity.BaseEntity;
import com.lst.projectlib.entity.Msg;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Response;

/**
 * Created by sxy on 2017/11/2.
 */

public abstract class BankListCallBack extends MsgCallBack {
    @Override
    public Msg parseNetworkResponse(Response response, int i) throws Exception {
        Msg msg = super.parseNetworkResponse(response, i);
        JSONObject object = (JSONObject) msg.getData();
        if (object != null) {
            if (!object.isNull("data")) {
                JSONArray array = object.getJSONArray("data");
                if (array != null && array.length() > 0) {
                    List<BaseEntity> list = new ArrayList<>();
                    for (int j = 0; j < array.length(); j++) {
                        JSONObject jsonObject = array.getJSONObject(j);
                        BankEntity entity = new BankEntity();
                        if (!jsonObject.isNull("bankCode")) {
                            entity.setBankCode(jsonObject.getString("bankCode"));
                        }
                        if (!jsonObject.isNull("dailyTradingLimit")) {
                            entity.setDailyTradingLimit(jsonObject.getString("dailyTradingLimit"));
                        }
                        if (!jsonObject.isNull("monthlyTransactionLimit")) {
                            entity.setMonthlyTransactionLimit(jsonObject.getString("monthlyTransactionLimit"));
                        }
                        if (!jsonObject.isNull("singleTransactionLimit")) {
                            entity.setSingleTransactionLimit(jsonObject.getString("singleTransactionLimit"));
                        }
                        if (!jsonObject.isNull("bankCode")) {
                            entity.setId(jsonObject.getString("bankCode"));
                        }
                        if (!jsonObject.isNull("bankName")) {
                            entity.setName(jsonObject.getString("bankName"));
                        }
                        list.add(entity);
                    }
                    msg.setData(list);
                    msg.setResult(array.toString());
                }
            }
        }
        return msg;
    }
}
